<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>测试页</title>
</head>

<body>
  <!-- <div>
    <input type="text" placeholder="输入手机号" id="uphone">
    <input type="password" placeholder="输入密码" id="upwd">
    <input type="button" value="登录" id="login">
  </div> -->
  <div>
    <input type="text" placeholder="用户名" id="uname">
    <input type="text" placeholder="输入手机号" id="uphone">
    <input type="password" placeholder="输入密码" id="upwd">
    <input type="button" value="注册" id="register">
  </div>
  <script>
    //首页测试
    /*let orgURL = window.location.href;
    //'http://127.0.0.1:9000/html/test.html?s_id=8' 首页跳转过来的链接 首页自己拼接的
    //'http://127.0.0.1:9000/html/story.html?s_id=8' 故事页或者分页跳转过来的链接
    function getURL(url){
      //截取地址中s_id=xxx
      let str = url.match(/s_id=\d+/)[0];
      //获取s_id中xx的值
      let num = str.split("=")[1];
      return num
    };
    let s_id = getURL(orgURL);
    let url = `/admin/storylist/${s_id}`
    let pre = document.querySelector("div pre");
    //promise封装ajax请求
    let getData = function (url){
      return new Promise(
        function (resolve,reject){
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function (){
        if(xhr.readyState===4) {
          if (xhr.status >= 200 && xhr.status < 300){
            let result = JSON.parse(xhr.responseText);
            // console.log(result);
              resolve(result);
          }else {
            reject(new Error("Request was unsuccessful: ")+ xhr.status);
          }
        }
      }
        xhr.open("GET",url,true);
        xhr.send();
      }
          );
        };
    //url地址传入接口拿到数据
    getData(url).then((result)=>{
        console.log(result);
        pre.innerHTML = result.data[0].s_text;
    });*/
  </script>
  <script>
   /* //登录js
    //promise封装post 方法ajax请求，传入url和data数据
    let postData = function (url,data) {
      return new Promise(
        function (resolve, reject) {
          let xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
              if (xhr.status >= 200 && xhr.status < 300) {
                let result = JSON.parse(xhr.responseText);
                //console.log(result);
                resolve(result);
              } else {
                reject(new Error("Request was unsuccessful: ") + xhr.status);
              }
            }
          }
          xhr.open("POST", url, true);
          xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
          xhr.send(data);
        }
      );
    };
    //登录a标签点击的时候获取用户输入的账户密码，验证后传入
    let login = document.querySelector("#login")
    let uphone = document.querySelector("#uphone")
    let upwd = document.querySelector("#upwd")
    login.onclick = function(){
      postData("/user/login",`u_phone=${uphone.value}&u_pwd=${upwd.value}`).then((result)=>{
        //console.log(result);
        if (result.code === 200){
          //1秒之后跳转
          setTimeout(()=>{
            window.location.href = "../index.html";
            // storage.setItem("u_name","某某用户"); 缓存，页头拿到后渲染 
            let storage = window.sessionStorage;
            storage.setItem("u_name",`${result.data[0].u_name}`)
        },1*1000)
        }else{
         //console.log("手机号或密码不对");
         return
        }
      })
    }*/
  </script>
  <script>
    //注册js
    //promise封装post 方法ajax请求，传入url和data数据
    let postData = function (url,data) {
      return new Promise(
        function (resolve, reject) {
          let xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
              if (xhr.status >= 200 && xhr.status < 300) {
                let result = JSON.parse(xhr.responseText);
                //console.log(result);
                resolve(result);
              } else {
                reject(new Error("Request was unsuccessful: ") + xhr.status);
              }
            }
          }
          xhr.open("POST", url, true);
          xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
          xhr.send(data);
        }
      );
    };
    //登录a标签点击的时候获取用户输入的账户密码，验证后传入
    let login = document.querySelector("#register")
    let uphone = document.querySelector("#uphone")
    let uname = document.querySelector("#uname")
    let upwd = document.querySelector("#upwd")
    register.onclick = function(){
      if(uphone.value && uname.value && upwd.value){
        postData("/user/register",`u_phone=${uphone.value}&u_pwd=${upwd.value}&u_name=${uname.value}`).then((result)=>{
        console.log(result);
        if (result.code === 200){
          //1秒之后跳转
          setTimeout(()=>{
            window.location.href = "./login.html";
            },1*1000)
        }else{
         //console.log("手机号或密码不对");
         return
        }
      })
    }
  }
  </script>
</body>

</html>